package org.example.xsda.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import org.example.xsda.common.PageResult;
import org.example.xsda.common.Result;
import org.example.xsda.dto.FileAuditDTO;
import org.example.xsda.dto.FileUploadDTO;
import org.example.xsda.service.FileService;
import org.example.xsda.vo.FileInfoVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * 文件管理Controller
 */
@RestController
@RequestMapping("/file")
public class FileController {
    
    @Resource
    private FileService fileService;
    
    /**
     * 上传单个文件
     */
    @PostMapping("/upload")
    public Result<FileInfoVO> uploadFile(
            @RequestParam("file") MultipartFile file,
            @RequestParam(required = false) String fileType,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) Long studentId,
            @RequestParam(required = false) Long approvalId,
            @RequestParam(required = false) Integer approvalRequired
    ) {
        FileUploadDTO dto = new FileUploadDTO();
        dto.setFileType(fileType != null ? fileType : "other");
        dto.setCategory(category != null ? category : "archive");
        dto.setStudentId(studentId);
        dto.setApprovalId(approvalId);
        dto.setApprovalRequired(approvalRequired);
        
        FileInfoVO result = fileService.uploadFile(file, dto);
        // 返回完整的文件信息对象
        return Result.success(result);
    }
    
    /**
     * 批量上传文件
     */
    @PostMapping("/upload/batch")
    public Result<List<FileInfoVO>> uploadFiles(
            @RequestParam("files") List<MultipartFile> files,
            @RequestParam(required = false) String fileType,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) Long studentId,
            @RequestParam(required = false) Long approvalId,
            @RequestParam(required = false) Integer approvalRequired
    ) {
        FileUploadDTO dto = new FileUploadDTO();
        dto.setFileType(fileType != null ? fileType : "other");
        dto.setCategory(category != null ? category : "archive");
        dto.setStudentId(studentId);
        dto.setApprovalId(approvalId);
        dto.setApprovalRequired(approvalRequired);
        
        List<FileInfoVO> result = fileService.uploadFiles(files, dto);
        return Result.success(result);
    }
    
    /**
     * 分页查询文件列表（管理员和教师）
     */
    @GetMapping("/page")
    @SaCheckRole(value = {"admin", "teacher"}, mode = SaMode.OR)
    public Result<PageResult<FileInfoVO>> getFilePage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) String fileType,
            @RequestParam(required = false) Integer auditStatus,
            @RequestParam(required = false) Long studentId
    ) {
        PageResult<FileInfoVO> result = fileService.getFilePage(current, size, category, fileType, auditStatus, studentId);
        return Result.success(result);
    }
    
    /**
     * 获取我上传的文件
     */
    @GetMapping("/my")
    public Result<PageResult<FileInfoVO>> getMyFiles(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String category
    ) {
        PageResult<FileInfoVO> result = fileService.getMyFiles(current, size, category);
        return Result.success(result);
    }
    
    /**
     * 获取待审核文件列表（管理员）
     */
    @GetMapping("/pending-audit")
    @SaCheckRole("admin")
    public Result<PageResult<FileInfoVO>> getPendingAuditFiles(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size
    ) {
        PageResult<FileInfoVO> result = fileService.getPendingAuditFiles(current, size);
        return Result.success(result);
    }
    
    /**
     * 审核文件（管理员）
     */
    @PostMapping("/audit")
    @SaCheckRole("admin")
    public Result<Void> auditFile(@RequestBody FileAuditDTO dto) {
        fileService.auditFile(dto);
        return Result.success();
    }
    
    /**
     * 获取文件详情
     */
    @GetMapping("/{id}")
    public Result<FileInfoVO> getFileDetail(@PathVariable Long id) {
        FileInfoVO result = fileService.getFileDetail(id);
        return Result.success(result);
    }
    
    /**
     * 删除文件
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteFile(@PathVariable Long id) {
        fileService.deleteFile(id);
        return Result.success();
    }
    
    /**
     * 获取学生的所有文件
     */
    @GetMapping("/student/{studentId}")
    @SaCheckRole(value = {"admin", "teacher"}, mode = SaMode.OR)
    public Result<List<FileInfoVO>> getStudentFiles(
            @PathVariable Long studentId,
            @RequestParam(required = false) String category
    ) {
        List<FileInfoVO> result = fileService.getStudentFiles(studentId, category);
        return Result.success(result);
    }
}

